//
//  SortTopViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SortTopViewController.h"
#import "SortTop.h"
#import "SortTopTableViewCell.h"
#import "BestMusicListViewController.h"
#import "MusicListViewController.h"


@interface SortTopViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)NSMutableDictionary *dic;
@property (nonatomic ,strong)NSMutableArray *arr;
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *nomalArr;
@property (nonatomic, strong)NSMutableArray *specielArr;
@property (nonatomic, assign)NSInteger pageId;
@property (nonatomic ,assign)NSInteger page;
@end

@implementation SortTopViewController
- (void)viewWillAppear:(BOOL)animated
{
//    self.pageId=1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self netWorking];
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
   
//    self.pageId = 1;
//    self.arr=[NSMutableArray array];
//    
//    
//    self.nomalArr = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=47; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__%ld.png", i]];
//        [self.nomalArr addObject:image];
//    }
    
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
//    self.specielArr = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%ld", i]];
//        [self.specielArr addObject:image];
//    }
//    // 遍历构造器初始化header
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//        [self.arr removeAllObjects];
//        self.pageId = 1;
//        [self netWorking];
//        [self.tableView.header endRefreshing];
//    }];
//    [header setImages:_nomalArr forState:MJRefreshStatePulling];
//    [header setImages:_specielArr forState:MJRefreshStateRefreshing];
//    
//    self.tableView.header = header;
//    
//    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//        self.pageId = _pageId + 1;
//        [self netWorking];
//        NSLog(@"%ld", _pageId);
//        footer.automaticallyChangeAlpha = YES;
//        [self.tableView.footer endRefreshing];
//    }];
//    self.tableView.footer = footer;
    
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self netWorking];
        [self.tableView.header endRefreshing];
        
        
    }];
    self.tableView.header=header;
    [self.tableView.header beginRefreshing];
   
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
      NSInteger i=self.arr.count/20;
        i++;
        
        [NetWorkManager GET:[[NSString stringWithFormat:@"%@%@%@",@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=iPhone&key=ranking%3Aalbum%3Aplayed%3A1%3A",self.str,[NSString stringWithFormat: @"&pageId=%ld&pageSize=20&position=0&title=%E6%8E%92%E8%A1%8C%E6%A6%9C",i]] stringByRemovingPercentEncoding]isCaChe:YES resultData:^(id responseObject) {
            
            self.dic=responseObject;
            NSMutableArray *a =[self.dic objectForKey:@"list"];
//            self.arr=[NSMutableArray array];
            for (NSMutableDictionary *d in a) {
                SortTop *top=[[SortTop alloc]init];
                [top setValuesForKeysWithDictionary:d];
                [self.arr addObject:top];
            }
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        } resultRrror:^(NSError *error) {
            
        }];

    }];
    self.tableView.footer = footer;

    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SortTopTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"top"];
    if (!cell) {
        cell=[[SortTopTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"top"];
        
    }
    SortTop *top=[self.arr objectAtIndex:indexPath.row];
    [cell.imageV1 sd_setImageWithURL:[NSURL URLWithString:top.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
    CGFloat f=indexPath.row;

    cell.lable1.text=[NSString stringWithFormat:@"%0.f",f+1];
    cell.lable2.text=top.title;
    cell.lable3.text=top.intro;
    cell.lable4.text=[NSString stringWithFormat:@"%@集",[top.tracks stringValue]];
    cell.imageV2.image=[UIImage imageNamed:@"album_tracks@3x.png"];
    return cell;
}


-(void)netWorking
{
    [NetWorkManager GET:[[NSString stringWithFormat:@"%@%@%@",@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=iPhone&key=ranking%3Aalbum%3Aplayed%3A1%3A",self.str,@"&pageId=1&pageSize=20&position=0&title=%E6%8E%92%E8%A1%8C%E6%A6%9C"] stringByRemovingPercentEncoding]isCaChe:YES resultData:^(id responseObject) {
       
        self.dic=responseObject;
      NSMutableArray *a =[self.dic objectForKey:@"list"];
        self.arr=[NSMutableArray array];
        for (NSMutableDictionary *d in a) {
            SortTop *top=[[SortTop alloc]init];
            [top setValuesForKeysWithDictionary:d];
            [self.arr addObject:top];
        }

        [self.tableView reloadData];
    } resultRrror:^(NSError *error) {
        
    }];

}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*80 + FM_Screen_Pts_Height*20;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicListViewController *best=[[MusicListViewController alloc]init];
    best.musicListDic=[[self.dic objectForKey:@"list"] objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:best animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
